package com.dwjm.shop.goods;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/deleteGoods")
public class DeleteGoodsHandler extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        Gson gson = new Gson();
        GoodsDao goodsDao = new GoodsDaoImpl();

        try {
            // 获取请求参数
            int id = Integer.parseInt(req.getParameter("id"));

<<<<<<< HEAD

=======
>>>>>>> 9734a825be80c2c805cf58c4a2752ba65a920899
            // 执行逻辑删除
            int affectedRows = goodsDao.deleteGoods(id);

            Map<String, Object> response = new HashMap<>();
            if (affectedRows > 0) {
                response.put("code", 200);
                response.put("msg", "商品已下架");
            } else {
                response.put("code", 404);
                response.put("msg", "商品不存在或更新失败");
            }
            resp.getWriter().write(gson.toJson(response));

        } catch (NumberFormatException e) {
            Map<String, Object> error = new HashMap<>();
            error.put("code", 400);
            error.put("msg", "参数格式错误");
            resp.setStatus(400);
            resp.getWriter().write(gson.toJson(error));
        } catch (Exception e) {
            Map<String, Object> error = new HashMap<>();
            error.put("code", 500);
            error.put("msg", "服务器内部错误: " + e.getMessage());
            resp.setStatus(500);
            resp.getWriter().write(gson.toJson(error));
        }
    }
}
